package org.jxc.web.action;

import java.util.Date;
import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.OutOrder;
import org.jxc.domain.OutOrderProduct;
import org.jxc.domain.Product;
import org.jxc.domain.Warehouse;
import org.jxc.service.OutOrderProductService;
import org.jxc.service.OutOrderService;
import org.jxc.service.SalesOrderService;
import org.jxc.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class OutOrderAction extends GenericAction<OutOrder> {
	private static final long serialVersionUID = -4665378081525232678L;
	@Autowired
	private OutOrderService outOrderService;
	@Autowired
	private OutOrderProductService outOrderProductService;

	@Autowired
	private StockService stockService;
	@Autowired
	private SalesOrderService salesOrderService;

	private List itemList;

	private OutOrder object;

	private List<OutOrderProduct> outOrderProducts;

	public String listPagination() throws Exception {
		page = outOrderService.listPagination(page);
		return SUCCESS;
	}

	public String save() throws CRUDException {
		object.setCreateTime(new Date());
		object.setCreator(LoginBean.getLoginBean().getUser());
		object.setAuditor(LoginBean.getLoginBean().getUser().getDepartment()
				.getManager());
		object.setDeleteFlag("0");
		outOrderService.save(object);
		if (outOrderProducts != null) {
			for (int i = 0; i < outOrderProducts.size(); i++) {
				OutOrderProduct temp = outOrderProducts.get(i);
				temp.setOutOrder(object);
				outOrderProductService.save(temp);
			}
		}
		return SUCCESS;
	}

	public String toSave() throws CRUDException {
		return SUCCESS;
	}

	public String view() throws CRUDException {
		object = outOrderService.findById(object.getId());
		itemList = outOrderProductService.findByOutOrderId(object.getId());
		return SUCCESS;
	}

	public String toCheck() throws CRUDException {
		object = outOrderService.findById(object.getId());
		itemList = outOrderProductService.findByOutOrderId(object.getId());
		return SUCCESS;
	}

	public String check() throws CRUDException {
		String auditState = object.getAuditState();
		Date outStockDate = object.getOutStockDate();
		object = outOrderService.findById(object.getId());
		object.setAuditState(auditState);
		object.setOutStockDate(outStockDate);
		outOrderService.update(object);

		if (auditState.equals(JXCString.T_CHECK)) {
			for (int i = 0; i < outOrderProducts.size(); i++) {
				OutOrderProduct ooProduct = new OutOrderProduct();
				ooProduct = outOrderProducts.get(i);
				stockService.update(ooProduct);
			}
			object.getSalesOrder().setOutState(JXCString.OUTSTORE);
			salesOrderService.updateSalesOrder(object.getSalesOrder());
		}
		return SUCCESS;
	}

	public String check2() throws CRUDException {
		String auditState = object.getAuditState();
		object = outOrderService.findById(object.getId());
		object.setAuditState(auditState);
		outOrderService.update(object);

		if (auditState.equals(JXCString.T_CHECK)) {
			outOrderProducts = outOrderProductService.findByOutOrderId(object
					.getId());
			for (int i = 0; i < outOrderProducts.size(); i++) {
				OutOrderProduct ooProduct = new OutOrderProduct();
				ooProduct = outOrderProducts.get(i);
				stockService.update(ooProduct);
			}
			if (object.getSalesOrder() != null) {
				object.getSalesOrder().setOutState(JXCString.OUTSTORE);
			}
		} else {
			if (object.getSalesOrder() != null) {
				object.getSalesOrder().setOutState(JXCString.B_OUTSTORE);
			}
		}

		if (object.getSalesOrder() != null) {
			salesOrderService.updateSalesOrder(object.getSalesOrder());
		}
		return SUCCESS;
	}

	public String toUpdate() throws CRUDException {
		object = outOrderService.findById(object.getId());
		itemList = outOrderProductService.findByOutOrderId(object.getId());
		return SUCCESS;
	}

	public String update() throws CRUDException {
		
		if(!outOrderProducts.isEmpty()){
			outOrderProductService.delByOutOrderId(object.getId());
			for (int i = 0; i < outOrderProducts.size(); i++) {
				OutOrderProduct temp = outOrderProducts.get(i);
				temp.setOutOrder(object);
				outOrderProductService.save(temp);
			}
		}
		
		
		Date outStockDate = object.getOutStockDate();
		object = outOrderService.findById(object.getId());
		object.setOutStockDate(outStockDate);
		outOrderService.update(object);

		
		return SUCCESS;
	}

	public String delete() throws CRUDException {
		outOrderService.delete(object.getId());
		return SUCCESS;
	}

	public List getItemList() {
		return itemList;
	}

	public void setItemList(List itemList) {
		this.itemList = itemList;
	}

	public OutOrder getObject() {
		return object;
	}

	public void setObject(OutOrder object) {
		this.object = object;
	}

	public List<OutOrderProduct> getOutOrderProducts() {
		return outOrderProducts;
	}

	public void setOutOrderProducts(List<OutOrderProduct> outOrderProducts) {
		this.outOrderProducts = outOrderProducts;
	}
}
